//
// Created on 2025/7/21.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#include "Timeline.h"
#include "Editor.h"
#include "ImageAsset.h"
#include "VideoAsset.h"
#include "AudioAsset.h"
#include "utils.h"

Timeline::Timeline() {}

std::shared_ptr<VideoAsset> Timeline::GetMainVideo() 
{
    return m_mainVideo;
}

std::shared_ptr<AudioAsset> Timeline::GetBgAudio()
{
    return m_bgAudio;
}

std::vector<std::shared_ptr<ImageAsset>> Timeline::GetImages()
{   
    std::lock_guard<std::mutex> lock(m_mutexList);
    return m_imagesList;
}

void Timeline::SetMainVideo(const std::string& path)
{
    std::shared_ptr<VideoAsset> mainVideo = std::make_shared<VideoAsset>(path);
    if (mainVideo == nullptr) {
        LOGE("main video is nullptr");
        return;
    }
    m_mainVideo = mainVideo; // 替换掉旧的mainVideo
}

void Timeline::SetBgAudio(const std::string& path)
{
    std::shared_ptr<AudioAsset> bgAudio = std::make_shared<AudioAsset>(path);
    m_bgAudio = bgAudio;
}

std::string Timeline::AddImage(const std::string& path)
{   
    std::lock_guard<std::mutex> lock(m_mutexList);
    std::shared_ptr<ImageAsset> image = std::make_shared<ImageAsset>(path);
    if (image == nullptr) {
        LOGE("Add Image failed.");
        return "";
    }
    m_imagesList.push_back(image);
    // 这里要图片解码加入渲染引擎，也可以放在导出时再加，因为图片预览是由前端控制的。
    LOGI("Add Image id = %{public}s.", image->GetId().c_str());
    return image->GetId();
}

void Timeline::RemoveImage(const std::string& imageId)
{   
    std::lock_guard<std::mutex> lock(m_mutexList);
    m_imagesList.erase(
        std::remove_if(m_imagesList.begin(), m_imagesList.end(), [&imageId](const auto& image) {
            if (imageId == image->GetId()) {
                LOGI("Find imageId %{public}s to be removed", imageId.c_str());
                return true;
            }
            return false;
        }),
        m_imagesList.end()
    ); 
}

void Timeline::UnsetBgAudio()
{
    m_bgAudio = nullptr;
}


const uint64_t Timeline::GetVideoEndPos()
{   
    if (m_mainVideo == nullptr) 
        return 0;
    return m_mainVideo->GetEndPos();
}


const uint64_t Timeline::GetVideoStartPos()
{   
    if (m_mainVideo == nullptr) 
        return 0;
    return m_mainVideo->GetStartPos();
}

const uint64_t Timeline::GetDuration()
{
    if (m_mainVideo == nullptr)
        return 0;
    return m_mainVideo->GetDuration();
}

std::shared_ptr<ImageAsset> Timeline::GetImage(const std::string& imageId)
{   
    std::shared_ptr<ImageAsset> res;
    for (std::shared_ptr<ImageAsset> image: m_imagesList) {
        if (image == nullptr) {
            LOGE("image is nullptr");
            continue;
        }
        if (image->GetId() == imageId) {
            LOGI("find image %{public}s", imageId.c_str());
            res = image;
            break;
        }
    }
    if (res == nullptr) {
        LOGE("did not find image %{public}s", imageId.c_str());
    }
    return res;
}